class player_item extends eui.Component implements eui.UIComponent {
	public constructor() {
		super();

		this.skinName = "resource/skins/items/player_item.exml";

	}

	lbRank: eui.Label;
	lbID: eui.Label;
	lbAttack: eui.Label;
	lbLv: eui.Label;
	imgAvatar: eui.Image;
	imgAvatarBorder: eui.Image;
	pgbHp: eui.ProgressBar;
	pgbExp: eui.ProgressBar;
	maskShape: egret.Shape;
	group: eui.Group;
	imgShield: eui.Image;
	imgWeapon: eui.Image;
	imgWeaponPlate: eui.Image;
	imgWeaponPos: egret.Point;


	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}

	protected childrenCreated(): void {

		super.childrenCreated();
		this.imgWeaponPos = new egret.Point(this.imgWeaponPlate.x + this.imgWeaponPlate.width / 2, this.imgWeaponPlate.y + this.imgWeaponPlate.height / 2);
		this.addMaskShape(0xA7E8E3, this.imgAvatar.x, this.imgAvatar.y + this.group.y, this.imgAvatar.width, this.imgAvatar.height);

	}



	public async refresh() {
		this.pgbHp.maximum = GameGlobal.ins().user.player.maxHp;
		this.pgbHp.minimum = 0;
		this.pgbHp.value = this.pgbHp.maximum;
		this.pgbExp.maximum = GameGlobal.ins().user.player.exp;
		this.pgbExp.minimum = 0;
		this.pgbExp.value = GameGlobal.ins().user.player.curExp;
		this.lbID.text = GameGlobal.ins().user.nickName;
		this.lbLv.text = GameGlobal.ins().user.player.level;
		this.lbRank.text = "12";
		this.lbAttack.text = GameGlobal.ins().user.player.attack;

		let data = await RES.getResByUrl("resource/art/equipm/" + GameGlobal.ins().user.player.equipms[0].id + ".png", null, null, RES.ResourceItem.TYPE_IMAGE);
		this.imgWeapon.width = data.textureWidth;
		this.imgWeapon.height = data.textureHeight;
		this.imgWeapon.texture = data;




		let data2 = await RES.getResByUrl("resource/art/equipm/" + GameGlobal.ins().user.player.equipms[1].id + ".png", null, null, RES.ResourceItem.TYPE_IMAGE);
		this.imgShield.width = data2.textureWidth;
		this.imgShield.height = data2.textureHeight;
		this.imgShield.texture = data2;
		this.imgShield.x = this.imgWeaponPos.x - this.imgShield.width / 2;
		this.imgShield.y = this.imgWeaponPos.y - this.imgShield.height + this.imgWeaponPlate.height / 2 - this.imgShield.height / 2;

		// AssetsUtil.ins().loadOneImage("http://sedn.egret.com/ueditor/20150819/55d42fbca0208.png").then((img:egret.Bitmap)=>{
		// 	 this.imgAvatar.source=img.texture;
		// });
		// SceneManager.instance.mainScene.$children.every((e)=>{
		// 	console.log(e)
		// 	return true;
		// });
		if (SceneManager.instance.mainScene.getChildByName("weaponPlayer") == null) {
			this.imgWeapon.x = this.imgWeaponPos.x - this.imgWeapon.width / 2;
			this.imgWeapon.y = this.imgWeaponPos.y - this.imgWeapon.height + this.imgWeaponPlate.height / 1.5;

			let globalPos = this.imgWeapon.localToGlobal();
			SceneManager.instance.mainScene.addChild(this.imgWeapon);
			this.imgWeapon.x = globalPos.x;
			this.imgWeapon.y = globalPos.y;
		}

		// SceneManager.instance.mainScene.addChild(this.itemEnemy.imgWeapon);
	}

	public fightEnd(isDied) {
		let showPos: egret.Point = this.pgbExp.localToGlobal();
		showPos.x = this.pgbExp.width - 160;
		showPos.y -= 20;
		if (!isDied) {
			this.pgbExp.value += GameGlobal.ins().enemy.exp;
			EffectUtils.showTips("经验值+" + GameGlobal.ins().enemy.exp, 1, GameGlobal.ins().TextColors.blue, showPos);
			 EffectUtils.showTips("金币+" + GameGlobal.ins().enemy.money, 1, GameGlobal.ins().TextColors.golden, new egret.Point(showPos.x,showPos.y-50));
		}
		else {
			this.pgbExp.value -= GameGlobal.ins().enemy.exp;
			EffectUtils.showTips("经验值-" + GameGlobal.ins().enemy.exp, 1, GameGlobal.ins().TextColors.red, showPos);
		}
	}

	public async runAttack(target, hurt) {
		await EffectUtils.attack(this.imgWeapon, target, 1, -hurt);
	}

	public getHurt(hurt) {
		GameGlobal.ins().user.player.curHp -= hurt;
		this.pgbHp.value = GameGlobal.ins().user.player.curHp;
	}

	private addMaskShape(color: number, x: number, y: number, width: number, height: number) {
		if (this.maskShape == null) {
			this.maskShape = new egret.Shape();
		}

		this.maskShape.graphics.beginFill(color, 1);
		this.maskShape.graphics.drawRoundRect(x, y, width, height, width * 2, height * 2);
		this.maskShape.graphics.endFill();

		this.addChild(this.maskShape);
		this.imgAvatar.mask = this.maskShape;
	}


}

